package com.coollang.smartbra.presenter;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;

import com.coollang.smartbra.constant.Constants;
import com.coollang.smartbra.fragment.PersonalFragment;
import com.coollang.smartbra.model.BaseModel;
import com.coollang.smartbra.model.DeviceModel;
import com.coollang.smartbra.model.GoldAndActionModel;
import com.coollang.smartbra.model.UserGoalAndStrengthModel;

import java.util.List;
import java.util.Map;

import io.realm.RealmResults;

/**
 * 首页用户Fragment P层
 */
public class PersonalFragmentPresenter extends BasePresenter<BaseModel> {

    private PersonalFragment mPersonalFragment;

    public PersonalFragmentPresenter(PersonalFragment personalFragment) {
        super(BaseModel.class);
        mPersonalFragment = personalFragment;
    }

    @Override
    public void setRequestInfo() {

    }

    @Override
    protected Map<String, String> getData() {
        return null;
    }

    @Override
    protected void parsrData(BaseModel model) {

    }

    /**
     * 查看是否绑定设备
     */
    public boolean isBindDevice() {
        // 查询
        RealmResults<DeviceModel> results = mRealm.where(DeviceModel.class).findAll();
        List<DeviceModel> deviceModels = mRealm.copyFromRealm(results);

        if (deviceModels.size() == 0) {
            return false;
        } else {
            return true;
        }
    }

    public UserGoalAndStrengthModel queryGoalAndStrength() {
        return mRealm.where(UserGoalAndStrengthModel.class).findFirst();
    }

    /**
     * 跳转到游戏
     */
    public void goGame() {
        Context context = mPersonalFragment.getContext();
        PackageManager packageManager = context.getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(Constants.GAME_PKG);
        context.startActivity(intent);
    }

    public GoldAndActionModel queryGoldAndAction() {
        return mRealm.where(GoldAndActionModel.class).findFirst();
    }
}
